#include "widgetchart.h"
#include <math.h>


WidgetChart::WidgetChart(QDeclarativeItem *parent) :
    QDeclarativeItem(parent)
{
    setFlag(QGraphicsItem::ItemHasNoContents, false);
    m_typeChart = 0;

}

WidgetChart::~WidgetChart()
{

}

void WidgetChart::setMoneyList(QList<QObject *> moneyList)
{
    qDebug() << __PRETTY_FUNCTION__;
    m_moneyList.clear();
    foreach (QObject* obj, moneyList) {
        m_moneyList.append((Money*)obj);
    }

    if (m_moneyList.length() > 0) {
        m_chart = new THLineGraph;

        Money *firstMoney = m_moneyList.at(0);
        QDate lastdate = firstMoney->getDtTransaction();
        double total = 0;
        foreach(Money* money, m_moneyList) {
            if (money->getDtTransaction().day() == lastdate.day() &&
                money->getDtTransaction().month() == lastdate.month() &&
                money->getDtTransaction().year() == lastdate.year()) {
                total = total + (money->getTypeMoney() == Money::type_in? 1: -1) * money->getAmount();
            }
            else {
                qDebug() << __PRETTY_FUNCTION__ << lastdate.toString("dd MMM") << QString::number(total);
                m_chart->append(THLineGraphData(lastdate.toString("dd MMM"),
                                               (total < 0? "-": "") + QString("R$") + QString::number(abs(total))));
                lastdate = money->getDtTransaction();
                total = total + (money->getTypeMoney() == Money::type_in? 1: -1) * money->getAmount();
            }
        }

        m_chart->append(THLineGraphData(lastdate.toString("dd MMM"),
                                        (total < 0? "-": "") + QString("R$") + QString::number(abs(total))));

        m_chart->setBackground(QColor(255,255,255));
        m_chart->setTextColor(QColor(0,0,0));

        m_chart->setSortY(true);
//        m_chart->setSortX(true);

        m_chart->resize(437,202);
        m_chart->setMinimumWidth(437);
    }

}

QList<QObject*> WidgetChart::moneyList()
{
    QList<QObject*> objList;
    foreach(Money* money, m_moneyList)
        objList.append(money);

    return objList;
}

void WidgetChart::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    //qDebug() << __PRETTY_FUNCTION__;
    if (m_typeChart == 0) {
        if (m_chart && !m_moneyList.isEmpty())
            // Line Chart about cash flow by time
            painter = m_chart->paintMe(painter, QSize(437, 202));
    } else {
        // Pie Chart showing total expenses by category
        painter = m_pieChart->paintMe(painter);
    }

}


void WidgetChart::setTypeChart(int typeChart)
{
    m_typeChart = typeChart;
}


int WidgetChart::typeChart()
{
    return m_typeChart;
}

void WidgetChart::setCategoryList(QList<QObject *> categoryList)
{
    m_categoryList.clear();
    foreach (QObject *obj, categoryList) {
        m_categoryList.append((MoneyCategory*)obj);
    }

    float total = 0;
    foreach (MoneyCategory* cat, m_categoryList) {
        total = total + cat->totalExpenses();
    }

    m_pieChart = new Nightcharts();

    if (total != 0) {
        m_pieChart->setType(Nightcharts::Dpie);
        m_pieChart->setLegendType(Nightcharts::Round);//{Round,Vertical}
        m_pieChart->setCords(20, 20, (340)/1.5, (124)/1.5);

        foreach (MoneyCategory* cat, m_categoryList) {
            m_pieChart->addPiece(cat->name(), cat->colorRgb(),
                                 cat->percentage());
        }
    }
    // m_pieChart->setFont(QFont(painter->font().family(), 6));
     m_pieChart->draw(QSize(250, 202));
    // m_pieChart->drawLegend();


}

QList<QObject*> WidgetChart::categoryList()
{
    QList<QObject*> objList;
    foreach(MoneyCategory *cat, m_categoryList)
        objList.append(cat);

    return objList;
}
